#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "nvs_flash.h"
#include <stdio.h>
#include <string.h>

#include "led.h"
#include "key.h"
#include "exit.h"
#include "uart.h"
#include "esptimer.h"
#include "gtimer.h"
#include "max31865.h"

#define SELECT 7

void app_main(void)
{
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||
        ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
    {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ESP_ERROR_CHECK(nvs_flash_init());
    }
// LED
#if SELECT == 1
    led_init();
    while (1)
    {
        printf("LED BLINK\n");
        LED_TOGGLE();
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
#endif
// KEY
#if SELECT == 2
    led_init();
    key_init();
    while (1)
    {
        switch (key_scan(0))
        {
        case KEY_GPIO_PRES:
        {
            LED_TOGGLE();
            break;
        }
        default:
            break;
        }
        vTaskDelay(pdMS_TO_TICKS(20));
    }
#endif
// EXIT
#if SELECT == 3
    led_init();
    exit_init();
    while (1)
    {
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
#endif
// UART
#if SELECT == 4
    led_init();
    uart_init(115200);
    uint32_t length;
    char buffer[UART_BUFF_SIZE] = {0};
    uint32_t times = 0;
    while (1)
    {
        uart_get_buffered_data_len(UART_UX, &length);
        if (length > 0)
        {
            memset(buffer, 0, UART_BUFF_SIZE);
            uart_read_bytes(UART_UX, buffer, length, 1000);
            uart_write_bytes(UART_UX, buffer, strlen(buffer));
        }
        else
        {
            if (times % 3000 == 0)
            {
                printf("HVP\n");
            }
            if (times % 1000 == 0)
            {
                printf("UART TEST\n");
            }
            if (times % 100 == 0)
            {
                LED_TOGGLE();
            }
        }
        vTaskDelay(pdMS_TO_TICKS(10));
        times++;
    }
#endif
// ESPTIMER
#if SELECT == 5
    led_init();
    esptimer_init(1000000);
#endif
// GTIMER
#if SELECT == 6
    gptimer_event_t g_tim_evente;
    uint8_t record;
    led_init();
    gtimer_init(100, 1000000);
    while (1)
    {
        record = 1;

        if (xQueueReceive(queue, &g_tim_evente, 2000))
        {
            ESP_LOGI("GPTIMER_ALARM", "定时器报警, 计数值： %llu", g_tim_evente.event_count); /* 打印通用定时器发生一次计数事件后获取到的值 */
            record--;
        }
        else
        {
            ESP_LOGW("GPTIMER_ALARM", "错过一次计数事件");
        }
    }
    vQueueDelete(queue);

#endif
// MAX31865
#if SELECT == 7

    // 初始化SPI
    spi2_init();
    // 配置MAX31865
    spi_device_handle_t max31865_handle = max31865_init(MAX31865_CS_GFIO_PIN, 250000, true, true, false, false);
    // 读取温度数据
    while (1)
    {
        float rtd_value = max31865_read_rtd(max31865_handle);
        uint8_t fault = max31865_read_fault(max31865_handle);
        float temperature = max31865_read_ResistanceToC(rtd_value);
        ESP_LOGI(TAG, "RTD值: %f, 温度值：%f, 故障状态: 0x%02X", rtd_value,temperature, fault);
        vTaskDelay(pdMS_TO_TICKS(1000));
    }

#endif
}
